<?
/*--|	NOTIFICATION
  Software: Socially
   Version: 1.0
   Contact: http://socially.googlecode.com
      Info: http://socially.googlecode.com
   Support: http://socially.googlecode.com
--------------------------------------------------------------------------
    Author: goker.cebeci | http://gokercebeci.com
--------------------------------------------------------------------------
   License: Distributed under the GNU General Public License (GPL)
            http://www.gnu.org/licenses/gpl.html
 This program is distributed in the hope that it will be useful - WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.
-------------------------------------------------------------------------*/
class notification {
	public static function send($a){		
		if(!$a['application']) return common::result(400);
		if(!$a['notification']) return common::result(401);
		if(!$a['sender']) return common::result(402);
		if(!$a['recipient']) return common::result(403);
		db::query(sprintf("INSERT INTO postieNotifications (application,notification,sender,recipient,item,date,status) VALUES ('%s','%s','%d','%d','%d',NOW(),'%s');"
			,$a['application'],$a['notification'],$a['sender'],$a['recipient'],$a['item'],'unread'));
		return common::result(200);
	}
	public static function mail($a){		
		if(!$a['application']) return common::result(400);
		if(!$a['message']) return common::result(401);
		if(!$a['sender']) return common::result(402);
		if(!$a['recipient']) return common::result(403);
		$recipient = account::get(array('ID'=>$a['recipient']));		
		$title = _('Notification');
		$data = sprintf(_('
		Hi %s;<br />
		<p><br />%s</p>
		<p><br />The %s team.<br /><br />
		Please do not reply to this message; it was sent from an unmonitored email address.<br />
		This message is a service email related to your use of %s.</p>')
		,'<strong>'.$recipient->name.'</strong>'
		,$a['message']
		,NAME,NAME);
		if(common::mail($recipient->name,$recipient->email,$title,$data)){
			return common::result(200);
		} else return common::result(600);
	}
	public static function get($a){
		$c = db::query(sprintf("SELECT * FROM postieNotifications WHERE recipient = %d ORDER BY `date` DESC LIMIT %s;",$a['UID'],$a['limit']),true,true);
		if($c) foreach ($c as $k=>$i){
			//$c[$k]->message = $i->application::notification($i->notification,account::get(array('ID'=>$i->sender))); php 5.3
			$app = $i->application;
                        if(class_exists($app)){
                            $c[$k]->message = $app::notification($i->notification,profile::get(array('ID'=>$i->sender)),$i->item);
                            $c[$k]->date = common::timediff($i->date);
                        } else unset($c[$k]);
		}		
		return $c;		
	}
	public static function invisible($a){
		if($a['sender']) db::query(sprintf("UPDATE postieAttendees SET status = 2 WHERE senderID = %d AND conversationID = %d;",$a['UID'],$a['CID']));
		else db::query(sprintf("UPDATE postieAttendees SET status = 2 WHERE recipientID = %d AND conversationID = %d;",$a['UID'],$a['CID']));
		return common::result(200, array(303,$a['UID'],0,$a['CID']));
	}	
	
	public static function unread($a){
		return db::query(sprintf("SELECT COUNT(ID) as unread FROM postieAttendees  WHERE recipientID = %d AND status = 0;",$a),true);
	}
	
}
?>